//
//  TLAleartViewCenterTwo.swift
//  TAleartView
//
//  Created by 唐基林 on 2025/1/21.
//

import UIKit

class TLAleartViewCenterTwo: UIView {

    var dismissHandler: (() -> Void)?
    
    var okButtonClickHandler: ((String) -> Void)?
    
    /// 标题
     var titel: String
     
     /// 提示内容
     var content: String
     
     /// 取消
     var cancel: String
     
     /// 确定
     var okStr: String
    
    init(frame: CGRect, titel: String, content: String, cancel: String, okStr: String) {
        self.titel = titel
        self.content = content
        self.cancel = cancel
        self.okStr = okStr
        super.init(frame: frame)
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var centerView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 15;
        return view
    }()
    
    lazy var titelab:UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = UIFont.boldSystemFont(ofSize: 15)
        lab.textAlignment = .center
        return lab;
    }()
    
    lazy var contentLab:UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.textAlignment = .left
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var lineView:UIView = {
        let vie = UIView()
        vie.backgroundColor = UIColor(rgb: 238, 236, 238,alpha: 0.8)
        return vie
    }()
    
    lazy var cancelBtn:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle(self.cancel, for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
        return btn
    }()
    
    lazy var okButton:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle(self.okStr, for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.addTarget(self, action: #selector(okbuttonClick), for: .touchUpInside)
        return btn
    }()
}

extension TLAleartViewCenterTwo {
    private func setUI(){
        self.addSubview(centerView)
        let (_,height) =  getDeviceWidthAndHeight()
        centerView.snp.makeConstraints { make in
            make.height.equalTo(height * 0.25)
            make.width.equalTo(height * 0.35)
            make.center.equalTo(self)
        }
        self.titelab.text = self.titel
        centerView.addSubview(self.titelab)
        self.titelab.snp.makeConstraints { make in
            make.left.equalTo(centerView).offset(20)
            make.right.equalTo(centerView).offset(-20)
            make.top.equalTo(centerView).offset(20)
            make.height.equalTo(18)
        }
        self.contentLab.text = self.content
        centerView.addSubview(self.contentLab)
        contentLab.snp.makeConstraints { make in
            make.left.equalTo(centerView).offset(20)
            make.right.equalTo(centerView).offset(-20)
            make.top.equalTo(titelab.snp_bottomMargin).offset(20);
        }
        
        centerView.addSubview(self.lineView)
        self.lineView.snp.makeConstraints { make in
            make.left.right.equalTo(centerView)
            make.height.equalTo(1)
            make.bottom.equalTo(centerView).offset(-50)
        }
        
        centerView.addSubview(self.cancelBtn)
        self.cancelBtn.snp.makeConstraints { make in
            make.left.bottom.equalTo(centerView)
            make.top.equalTo(self.lineView.snp_bottomMargin)
            make.width.equalTo(centerView.snp.width).dividedBy(2)
        }
        
        centerView.addSubview(self.okButton)
        self.okButton.snp.makeConstraints { make in
            make.left.equalTo(self.cancelBtn.snp_rightMargin)
            make.bottom.right.equalTo(centerView)
            make.top.equalTo(self.lineView.snp_bottomMargin)
        }
    }
    
    private func removeView() {
        UIView.animate(withDuration: 0.3) {
            self.alpha = 0.0
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
    
}

extension TLAleartViewCenterTwo {
    @objc func cancelBtnClick(){
        if (self.dismissHandler != nil) {
            dismissHandler?()
        }
        self.removeView()
    }
    
    @objc func okbuttonClick() {
        if (self.okButtonClickHandler != nil) {
            self.okButtonClickHandler?("ok")
        }
        self.removeView()
    }
}
